package com.yatuhashi.manual.mhp3;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.yatuhashi.manual.mhp3.bean.QuestDetailBean;
import com.yatuhashi.manual.mhp3.db.DataBaseHelper;

public class QuestDetailActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quest_detail_activity);
        
        initHeader("クエスト詳細      ");
        
        initDetail();
    }
    
    @Override
    public void onClick(View v) {
    	switch (v.getId()) {
		case R.id.top_icon:
			finish();
			break;
		case R.id.app_title:
			break;
		case R.id.left_btn:
			toast("search button");
			break;
		case R.id.right_btn:
			toast("share button");
			break;
		default:
			break;
		}
    }
    
    private void initDetail() {
        String qid = getIntent().getStringExtra("qid");
        QuestDetailBean quest = getDetail(qid);
        
        TextView textView = new TextView(this);
        textView.setTextColor(R.color.black);
        textView.setGravity(Gravity.CENTER);
        
        TextView qtitle = (TextView) findViewById(R.id.qtitle);
        qtitle.setText(quest.getTarget());
        
        TextView qtype = (TextView) findViewById(R.id.qtype);
        qtype.setText(quest.getType());

        TextView qstar = (TextView) findViewById(R.id.qstar);
        qstar.setText(quest.getStar());
    }
    
    private QuestDetailBean getDetail(String qid) {
    	DataBaseHelper dbHelper = new DataBaseHelper(this);
    	SQLiteDatabase db = dbHelper.getWritableDatabase();
    	
    	QuestDetailBean quest = new QuestDetailBean();
    	String sql =
    		"select quest_table.type, quest_table.star, quest_detail_table.target, " +
    		"quest_detail_table.reward, quest_detail_table.deposit, quest_detail_table.monster, " +
    		"quest_detail_table.habitat, quest_detail_table.limittime, quest_detail_table.mid " + 
    		"from quest_table join quest_detail_table on quest_table.qid = quest_detail_table.qid " +
    		"where quest_table.qid = '" + qid + "'";
    	
    	Cursor cur = db.rawQuery(sql, null);
    	if(cur.moveToFirst()) {
    		do {
    			quest.setType(cur.getString(cur.getColumnIndex("type")));
    			quest.setStar(cur.getString(cur.getColumnIndex("star")));
    			quest.setTarget(cur.getString(cur.getColumnIndex("target")));
    			quest.setReward(cur.getString(cur.getColumnIndex("reward")));
    			quest.setDeposit(cur.getString(cur.getColumnIndex("deposit")));
    			quest.setMonster(cur.getString(cur.getColumnIndex("monster")));
    			quest.setHabitat(cur.getString(cur.getColumnIndex("habitat")));
    			quest.setLimit(cur.getString(cur.getColumnIndex("limittime")));
    			break;
    		} while(cur.moveToNext());
    	}
    	cur.close();
    	db.close();
    	return quest;
    }
}
